from collections import deque

"""将列表当做堆栈使用"""
stack = [3, 4, 5]
# append() 方法可以把一个元素添加到堆栈顶
stack.append(6)
stack.append(7)
print(stack)
# 用pop() 方法返回最后一个元素，实现最先进入的元素最后一个被释放（后进先出）
stack.pop()

"""将列表当做队列使用"""
queue=deque(["Eric","John","ABC"])  # 把列表转换成队列
queue.append("baidu")
queue.append("google")
queue.popleft()   # 用popleft()方法取出队列里的第一个元素（先进先出）
print(queue)

"""列表推导式"""
vec = [2,4,6]
list1=[[x,x**2] for x in vec]
print("list1,列表:",list1)
"""用 if 子句作为过滤器"""
list2=[3*x for x in vec if x>3]
print("list2,x>3:",list2)
list3=[3*x for x in vec if x<3]
print("list3,x<3:",list3)
"""for循环"""
vec1 = [2, 4, 6]
vec2 = [4, 3, -9]
list4=[x*y for x in vec1 for y in vec2]
print("list4相乘:",list4)
list5=[x+y for x in vec1 for y in vec2]
print("list5相加:",list5)
list6=[vec1[i]*vec2[i]for i in range(len(vec1))]
print("list6列表1和列表2对应元素相乘",list6)
"""使用复杂表达式或嵌套函数"""
list7=[str(round(355/113, i)) for i in range(1, 6)]
print("list7四舍五入后保留1-5位小数位：",list7)

"""嵌套列表"""
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
print("matrix:",matrix)
"""将3X4的矩阵列表转换为4X3列表"""
matrix_new=[[row[i] for row in matrix]for i in range(4)]
print("matrix_new:",matrix_new)
# 另外一种实现方法
matrix_new2=[]
for i in range(4):
    matrix_new2.append([row[i] for row in matrix])
print("方法2matrix_new2：",matrix_new2)

"""del语句"""
a = [-1, 1, 66.25, 333, 333, 1234.5]
# del a[2:4]  # 删除索引为2到3的元素
# print("a1:",a)
del a[:]  # 删除列表中所有元素
print("a2:",a)
# del a # 删除列表
# print("a3:",a)



